//
//  FeedViewController.m
//  iMADE
//
//  Created by Ed Knutson on 10/20/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "FeedViewController.h"

@implementation FeedViewController

@synthesize feedUrl, items, loader, myTitleView, prefix, statusMsg;

- (id)init {
	if(self = [super init]) {
		self.feedUrl = nil;
		self.prefix = nil;
		self.items = nil;
		self.statusMsg = @"Feed not loaded";
	}
	return self;
}

- (id)initWithFeedUrl:(NSString *)url {
	if(self = [self init])
		self.feedUrl = url;
	return self;
}

- (void)loadFeed {
	if(prefix == nil || feedUrl == nil)
		return;
	self.loader = [[JSONDownloader alloc] init];
	[loader release];
	[loader downloadJSON:[prefix stringByAppendingString:feedUrl] delegate:self];
}

- (void)downloadJSONFinished:(NSDictionary *)dict {
	NSLog(@"parsing JSON finished");
	NSArray *objects = [dict valueForKey:@"json"];
	if(objects == nil || [objects count] == 0) {
		self.items = [[[NSArray alloc] init] autorelease];
		statusMsg = @"Feed empty";
		return;
	} else self.items = objects;
	statusMsg = @"Feed loaded";
	[self processItems];
}

- (void)downloadJSONFailed:(NSString *)str {
	NSLog(@"download JSON failed");
	statusMsg = str;
}

- (void)parsingJSONFailed:(NSString *)str {
	NSLog(@"parsing JSON failed");
	statusMsg = str;
}

- (void)processItems {
	[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}


#pragma mark Table view methods

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return self.title;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	cell.textLabel.text = statusMsg;
    return cell;
}


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[[NSBundle mainBundle] loadNibNamed:@"MyGroupsTitleView" owner:self options:nil];
	self.navigationItem.titleView = myTitleView;
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	if(items == nil) {
		[self loadFeed];
	}
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[prefix release];
	[loader release];
	[statusMsg release];
	[items release];
	[feedUrl release];
    [super dealloc];
}


@end
